namespace ClassyBlog.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using DomainObjects;

    public static class RepositoryExtensions
    {
        public static int Count(
            this IPageRepository instance, string relativePath)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var count = instance.FindAll(relativePath)
                                .AsQueryable()
                                .Count();

            return count;
        }

        public static int Count(this IPostRepository instance)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var count = instance.All()
                                .AsQueryable()
                                .Count();

            return count;
        }

        public static IEnumerable<Post> FindAll(
            this IPostRepository instance,
            DateTime startAt,
            DateTime endAt)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var posts = instance.All()
                                .AsQueryable()
                                .Where(post =>
                                    post.PublishedAt >= startAt &&
                                    post.PublishedAt < endAt);

            return posts;
        }

        public static int Count(
            this IPostRepository instance,
            DateTime startAt,
            DateTime endAt)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var count = instance.FindAll(startAt, endAt)
                                .AsQueryable()
                                .Count();

            return count;
        }

        public static IEnumerable<Post> FindAll(
            this IPostRepository instance,
            string tag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var posts = instance.All()
                                .AsQueryable()
                                .Where(post =>
                                    post.Tags.Any(t => t.Slug == tag));

            return posts;
        }

        public static int Count(
            this IPostRepository instance,
            string tag)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            var count = instance.FindAll(tag)
                                .AsQueryable()
                                .Count();

            return count;
        }
    }
}